/** Lachlan Hewson-Doherty (h201384171)
 *  Diploma of Digital and Interactive Games
 *  lachlan_doherty@live.com.au
 *  2013
 */

package cabbage.database.operations;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import cabbage.database.utilities.DatabaseConnection;






public class AccountCreationOps {
	
	
private DatabaseConnection db;

	
	
	
	
	public int insertNewAccount(String username, String firstname, String lastname, String emailaddress, String password) throws SQLException {
		
		
		db = new DatabaseConnection
						();
		
		Connection conn = db.getConnection
									();
		
		PreparedStatement ps = conn.prepareStatement
										("insert into account (username, firstname, lastname, emailaddress, password) values (?,?,?,?,?)", 
									Statement.RETURN_GENERATED_KEYS);
		
		ps.setString
				(1, username);
		
		ps.setString
				(2, firstname);

		ps.setString
				(3, lastname);

		ps.setString
				(4, emailaddress);

		ps.setString
				(5, password);

		ps.execute
				();
		
		ResultSet rs = ps.getGeneratedKeys
									();
		
		//Making sure that the 'ResultSet' in NOT null!
		if (rs != null && rs.next()) {
			return rs.getInt(1);
		}
		
		conn.close();
		
		return 0;

		
	}
	
	
	public int insertNewPlayer(int accountid) throws SQLException {
		
		
		db = new DatabaseConnection
						();
		
		Connection conn = db.getConnection
									();
		
		PreparedStatement ps = conn.prepareStatement
										("insert into account (username, firstname, lastname, emailaddress, password) values (?)", 
									Statement.RETURN_GENERATED_KEYS);
		
		ps.setInt
				(1, accountid);

		ps.execute
				();
		
		ResultSet rs = ps.getGeneratedKeys
									();
		
		//Making sure that the 'ResultSet' contains anything.
		if (rs.next()) {
			int newPlayerID = rs.getInt(1);
			return newPlayerID;
		}
		
		conn.close();
		
		return 0;

		
	}

}
